package com.bjpowernode.crm.workbench.controller;


import com.bjpowernode.crm.commons.contans.Contans;
import com.bjpowernode.crm.commons.domain.ReturnObject;
import com.bjpowernode.crm.commons.utils.DateUtils;
import com.bjpowernode.crm.commons.utils.UUIDUtils;
import com.bjpowernode.crm.settings.domain.User;
import com.bjpowernode.crm.settings.service.UserService;
import com.bjpowernode.crm.workbench.domain.Activity;
import com.bjpowernode.crm.workbench.domain.Customer;
import com.bjpowernode.crm.workbench.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/workbench/customer/")
public class CustomerController {

    @Autowired
    private UserService userService;

    @Autowired
    private CustomerService customerService;


    /**
     * 展示用户信息
     * @param request
     * @return
     */
    @RequestMapping("index.do")
    public String index(HttpServletRequest request){
        //调用service层的查询方法,查询所有用户
        List<User> userList = userService.queryAllUsers();
        //保存数据到request中
        request.setAttribute("userList",userList);
        //请求转发到市场活动页面
        return "workbench/customer/index";
    }

    /**
     * 保存用户信息
     * @param customer
     * @param session
     * @return
     */
    @RequestMapping("saveCreateCustomer.do")
    public @ResponseBody Object saveCreateCustomer(Customer customer, HttpSession session){
        //封装传入的参数(随机生成一个)
        customer.setId(UUIDUtils.getUUID());
        //传入系统当前时间
        customer.setCreateTime(DateUtils.formateDateTime(new Date()));
        //传入当前登录的用户
        User userName = (User)session.getAttribute(Contans.SESSION_USER);
        customer.setCreateBy(userName.getId());

        //封装返回信息
        ReturnObject returnObject = new ReturnObject();
        try {
            //调用service层方法,保存数据
            int ret = customerService.SaveCreateCustomer(customer);
            if(ret > 0){
                returnObject.setCode(Contans.RETURN_OBJECT_CODE_SUCCESS);
                returnObject.setMessage("添加成功");
                return returnObject;
            }else{
                returnObject.setCode(Contans.RETURN_OBJECT_CODE_FALSE);
                returnObject.setMessage("保存失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            returnObject.setCode(Contans.RETURN_OBJECT_CODE_FALSE);
            returnObject.setMessage("保存失败");
        }

        return returnObject;
    }


    @RequestMapping("queryCustomerByConditionForPAge.do")
    @ResponseBody
    public Object queryCustomerByConditionForPAge(String name,String owner,String phone,String website
            ,int pageNo,int pageSize){
        //封装参数
        Map<String, Object> map = new HashMap<>();
        map.put("name",name);
        map.put("owner",owner);
        map.put("phone",phone);
        map.put("website",website);
        map.put("beginNo",(pageNo-1)*pageSize);
        map.put("pageSize",pageSize);

        //调用service层方法去查数据
        List<Customer> customerList =customerService.queryCustomerByConditionForPage(map);
        //查询页面数量
        int totalRows = customerService.queryCountOfCustomerByCondition(map);

        //生成响应信息
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("customerList",customerList);
        resultMap.put("totalRows",totalRows);
        return resultMap;
    }

    /**
     * 批量删除数据
     * @param id
     * @return
     */
    @RequestMapping("deleteCustomerIds.do")
    @ResponseBody
    public Object deleteCustomerIds(String[] id){
        //创建一个返回对象
        ReturnObject returnObject = new ReturnObject();
        try {
            //调用service层方法去执行函数
            int res = customerService.deleteCustomerByIds(id);
            if (res > 0) {
                returnObject.setCode(Contans.RETURN_OBJECT_CODE_SUCCESS);
                returnObject.setMessage("删除成功！");
                return returnObject;
            } else {
                returnObject.setCode(Contans.RETURN_OBJECT_CODE_FALSE);
                returnObject.setMessage("删除失败！");
                return returnObject;
            }
        }catch (Exception e){
            e.printStackTrace();
            returnObject.setCode(Contans.RETURN_OBJECT_CODE_FALSE);
            returnObject.setMessage("删除失败！");
            return returnObject;
        }

    }

    /**
     * 根据id查询市场活动，然后在前端显示出来
     * @param id
     * @return
     */
    @RequestMapping("queryCustomerById.do")
    @ResponseBody
    public Object queryCustomerById(String id){
        //先查询有没有该市场活动，
        Customer customer = customerService.selectCustomerById(id);
        //获得Customerc对象并且封装好
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("id",customer.getId());
        resultMap.put("owner",customer.getOwner());
        resultMap.put("name",customer.getName());
        resultMap.put("phone",customer.getPhone());
        resultMap.put("website",customer.getWebsite());
        resultMap.put("address",customer.getAddress());
        resultMap.put("description",customer.getDescription());
        resultMap.put("contactSummary1",customer.getContactSummary());
        resultMap.put("nextContactTime2",customer.getNextContactTime());
        return resultMap;
    }


    @RequestMapping("saveEditCustomer.do")
    @ResponseBody
    public Object saveEditCustomer(Customer customer,HttpSession session) {
        //封装修改时间
        customer.setEditTime(DateUtils.formateDateTime(new Date()));
        User user = (User) session.getAttribute(Contans.SESSION_USER);
        //用id因为姓名不唯一
        customer.setEditBy(user.getId());
        //调用service层
        int i = customerService.saveEditCustomer(customer);
        //创建返回信息类
        ReturnObject returnObject = new ReturnObject();

        try {
            if (i > 0) {
                returnObject.setCode(Contans.RETURN_OBJECT_CODE_SUCCESS);
                returnObject.setMessage("更新成功！");
                return returnObject;
            } else {
                returnObject.setCode(Contans.RETURN_OBJECT_CODE_FALSE);
                returnObject.setMessage("更新失败！");
                return returnObject;
            }
        } catch (Exception e) {
            e.printStackTrace();
            returnObject.setCode(Contans.RETURN_OBJECT_CODE_FALSE);
            returnObject.setMessage("更新失败！");
            return returnObject;
        }
    }

}
